package org.rambadger.command.resource;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.yammer.dropwizard.auth.Auth;
import com.yammer.dropwizard.views.View;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.hibernate.validator.constraints.NotEmpty;
import org.rambadger.command.auth.AuthUser;
import org.rambadger.command.command.CreatePlatform;
import org.rambadger.command.validation.UrlIdentifier;
import org.rambadger.command.view.IndexView;
import org.rambadger.command.view.RegisterPlatformForm;


import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

@Path("/registerplatform")
public class RegisterPlatformResource {
    private @Context CommandGateway gateway = null;

    public static class PlatformSubmissionJSON {
        @Valid @NotEmpty @UrlIdentifier @JsonProperty
        String platformId;
        @Valid @NotEmpty  @JsonProperty
        String platformName;
        @Valid @NotEmpty @JsonProperty
        String Description;

    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void registerPlatform(@Auth(required = true) AuthUser user, @Valid PlatformSubmissionJSON newPlatformData) {

        //TODO: Check formatting of stuff
        if (user.hasAdmin())
        gateway.send(new CreatePlatform(newPlatformData.platformId, newPlatformData.platformName, newPlatformData.Description));

    }
    @GET
    @Produces(MediaType.TEXT_HTML)
    public View registerPlatformForm(@Auth(required = true) AuthUser user) {
        if (user.hasAdmin())
        return new RegisterPlatformForm(user);

        else
            return new IndexView(user);
    }

}
